package com.cz.kafka.Interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerInterceptor;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;

import java.util.Map;


@Slf4j
public class MyConsumerInterceptor implements ConsumerInterceptor<String, String> {


    @Override
    public ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String> consumerRecords) {
        log.info("onConsume 被调用");
        for (TopicPartition partition : consumerRecords.partitions()) {
            for (ConsumerRecord<String, String> record : consumerRecords.records(partition)) {
                log.info("收到消息: key = {}, value = {}, partition = {}, offset = {}",
                        record.key(), record.value(), record.partition(), record.offset());
            }
        }
        return consumerRecords;
    }

    @Override
    public void onCommit(Map<TopicPartition, OffsetAndMetadata> map) {
        log.info("onCommit:"+map.toString());
    }

    @Override
    public void close() {
        // 执行清理操作
    }

    @Override
    public void configure(Map<String, ?> configs) {
        // 配置拦截器（如果有需要）
    }
}
